import $ from 'jquery';

import { ClientAjax, CODE, MyAjax, API_URL } from '../config';
import { function_get_task } from './task';

export const set_deploy_url = 'set_deploy_url';
export const set_verify_deploy = 'set_verify_deploy';
export const set_hosts = 'set_project_hosts';
export const set_dbconnectors = 'set_project_dbconnectors';
export const add_hosts = 'add_project_host';
export const add_dbconnectors = 'add_project_dbconnectors';
export const set_branches = 'set_branches';
export const set_branches_servsid = 'set_branches_by_servsid';
export const set_databases = 'set_project_dbs';

export function function_set_deploy_url (ip, port, dispatch, cb) {
  let deployUrl = "http://" + ip + ":" + port + "/";
  ClientAjax(deployUrl, API_URL.ckVersion, {}, function () {
    MyAjax(API_URL.setClientUrl, { deployUrl: deployUrl }, function () {
      cb();
      dispatch({
        type: set_deploy_url,
        deployUrl: deployUrl
      });
    });
  });
}

export function function_diff_branch(deployUrl, projectId, taskId, servsId, cb){
  ClientAjax(deployUrl, API_URL.diffFileByService, { projectId, taskId, servsId }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data)
    }
  });
}

export function function_get_dbconnectors (deployUrl, projectStr, dispatch, cb) {
  ClientAjax(deployUrl, API_URL.lsProjectDb, { projectStr: projectStr }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: set_dbconnectors,
        list: result.data
      });
      cb(result.data);
    }
  });
}

export function function_get_logger (deployUrl, projectId, taskId, servsId, env, cb){
  ClientAjax(deployUrl, API_URL.getDeployLogger, { 
    projectId,
    taskId,
    servsId,
    env
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
    }
  });
}

export function function_set_task_failured(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.setTaskFailured, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, ()=>cb());
    }else{
      alert(result.message);
    }
  })
}

export function function_deploy_test_task(deployUrl, projectId, taskId, reloadFlg, cb){
  ClientAjax(deployUrl, API_URL.deployTaskTest, { 
    projectId,
    taskId,
    reloadFlg
  }, function (result) {
    cb();
  });
}

export function function_deploy_prev_task(deployUrl, projectId, taskId, reloadFlg, cb){
  ClientAjax(deployUrl, API_URL.deployTaskPre, { 
    projectId,
    taskId,
    reloadFlg
  }, function (result) {
    cb();
  });
}

export function function_deploy_pro_task(deployUrl, projectId, taskId, reloadFlg, cb){
  ClientAjax(deployUrl, API_URL.deployTaskOnline, { 
    projectId,
    taskId,
    reloadFlg
  }, function (result) {
    cb();
  });
}

export function function_deploying_task(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.setTaskDeploying, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, cb);
    }else{
      alert(result.message);
    }
  })
}

export function function_test_task(projectId, taskId, testHost, dispatch, cb){
  MyAjax(API_URL.setTaskTesting, {
    projectId,
    taskId,
    testHost
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, cb);
    }else{
      alert(result.message);
    }
  })
}

export function function_pre_test_task(projectId, taskId, prevHost, dispatch, cb){
  MyAjax(API_URL.setTaskPreTesting, {
    projectId,
    taskId,
    prevHost
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, cb);
    }else{
      alert(result.message);
    }
  })
}

export function function_test_success(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.setTaskTestSuccess, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, ()=>cb());
    }else{
      alert(result.message);
    }
  })
}

export function function_pre_test_success(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.setTaskPrevTestSuccess, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, ()=>cb());
    }else{
      alert(result.message);
    }
  })
}

export function function_test_abort(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.closeTest, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, ()=>cb());
    }else{
      alert(result.message);
    }
  })
}

export function function_pre_test_abort(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.closePreTest, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      function_get_task(projectId, taskId, dispatch, ()=>cb());
    }else{
      alert(result.message);
    }
  })
}

export function function_add_dbconnector (deployUrl, projectId, cb) {
  let dbConnectName = $("#dbConnectName").val();
  if (dbConnectName === "") return;
  let connectorType = $("#connectorType").val();
  let host = $("#host").val();
  if (host === "") return;
  let port = parseInt($("#port").val());
  if (typeof port !== 'number') {
    return;
  }
  let username = $("#username").val();
  if (username === "") {
    return;
  }
  let password = $("#password").val();
  if (password === "") {
    return;
  }

  let hostName = "";
  let isUseSsh = 0;
  if ($("#isUseSsh").is(':checked')) {
    isUseSsh = 1;
    hostName = $("#hostName").val();
  }

  ClientAjax(deployUrl, API_URL.addProjectDbConnector, {
    projectId: projectId,
    dbConnectName,
    connectorType,
    host,
    port,
    username,
    password,
    isUseSsh,
    hostName
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb();
    }
  });
}

export function function_remove_db_connector (deployUrl, projectId, cb) {
  if (projectId === "") return;
  let dbConnectorName = $("#dbConnectName").val();
  if (dbConnectorName === "") return;
  ClientAjax(deployUrl, API_URL.removeProjectDbConnector, {
    projectId: projectId,
    dbConnectorName: dbConnectorName
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb();
    }
  });
}

export function function_get_hosts (deployUrl, projectStr, dispatch, cb) {
  ClientAjax(deployUrl, API_URL.lsProjectHost, { projectStr: projectStr }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: set_hosts,
        list: result.data
      });
      cb(result.data);
    }
  });
}

export function function_remove_host (deployUrl, projectId, cb) {
  if (projectId === "") return;
  let hostName = $("#hostName").val();
  if (hostName === "") return;
  ClientAjax(deployUrl, API_URL.removeProjectHost, {
    projectId: projectId,
    hostName: hostName
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb();
    }
  });
}

export function function_add_host (deployUrl, projectId, cb) {
  if (projectId === "") return;
  let hostName = $("#hostName").val();
  if (hostName === "") return;
  let host = $("#host").val();
  if (host === "") return;
  let port = parseInt($("#port").val());
  if (typeof port !== 'number') {
    return;
  }
  let username = $("#username").val();
  if (username === "") {
    return;
  }
  let password = $("#password").val();
  let privateKeyPath = $("#privateKeyPath").val();
  if (password === "" && privateKeyPath === "") {
    alert("请填写密码或私钥路径");
    return;
  }
  ClientAjax(deployUrl, API_URL.addProjectHost, {
    projectId: projectId,
    hostName: hostName,
    host: host,
    port: port,
    username: username,
    password: password,
    privateKeyPath: privateKeyPath
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb();
    }
  });
}

export function function_get_branches_by_dir (deployUrl, directory, cb) {
  ClientAjax(deployUrl, API_URL.lsBranchesByDir, { directory: directory }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
    }
  });
}

export function function_get_branches_by_servsid (deployUrl, servsid, dispatch, cb) {
  ClientAjax(deployUrl, API_URL.lsBranchesByServsId, { servsId: servsid }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: set_branches_servsid,
        servsid: servsid,
        list: result.data
      });
      cb(result.data);
    }
  });
}

export function function_save_task (projectId, title, mark, servsId, branch, sqls, jobs, admin, tester, dispatch, cb) {
  MyAjax(API_URL.saveMergeRequest, {
    projectId,
    title,
    mark,
    servsId,
    branch,
    sqls,
    jobs,
    admin,
    tester
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
    }
  })
}

export function function_add_task (projectId, title, mark, servsId, branch, sqls, jobs, admin, tester, dispatch, cb) {
  MyAjax(API_URL.addMergeRequest, {
    projectId,
    title,
    mark,
    servsId,
    branch,
    sqls,
    jobs,
    admin,
    tester
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
    }
  })
}

export function function_save_task_from_trash (taskId, projectId, title, mark, servsId, branch, sqls, jobs, admin, tester, dispatch, cb) {
  MyAjax(API_URL.saveMergeRequestFromTrash, {
    taskId,
    projectId,
    title,
    mark,
    servsId,
    branch,
    sqls,
    jobs,
    admin,
    tester
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
    }
  })
}

export function function_add_task_from_trash (taskId, projectId, title, mark, servsId, branch, sqls, jobs, admin, tester, dispatch, cb) {
  MyAjax(API_URL.addMergeRequestFromTrash, {
    taskId,
    projectId,
    title,
    mark,
    servsId,
    branch,
    sqls,
    jobs,
    admin,
    tester
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
    }
  })
}